﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Reflection;

namespace MRS.WebUI.CustomerControl
{
    public partial class Pager : System.Web.UI.UserControl
    {
        log4net.ILog _logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AutoPageDataBind(PageSize);
            }
        }

        public delegate void SetFormat();
        public event SetFormat Format = null;

        public delegate IEnumerable TheData(int pageindex, int pagesize, out int totalcount);
        public event TheData GetData = null;

        /// <summary>
        /// 数据源reperter
        /// </summary>
        private Repeater repeater = null;
        public Repeater Repeater
        {
            get { return repeater; }
            set { repeater = value; }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        private int totalRecordCount;
        public int TotalRecordCount
        {
            get { return totalRecordCount; }
            set { totalRecordCount = value; }
        }

        /// <summary>
        /// 当前页索引
        /// </summary>
        public int CurPageIndex
        {
            get
            {
                if (ViewState["pageIndex"] != null)
                    return (int)ViewState["pageIndex"];
                return 0;
            }
            set
            {
                ViewState["pageIndex"] = value;
            }
        }

        /// <summary>
        /// 页大小
        /// </summary>
        public int PageSize
        {
            get
            {
                if (ViewState["pagesize"] != null)
                    return (int)ViewState["pagesize"];
                return Convert.ToInt32(this.drpPageSize.SelectedValue);
            }
        }


        /// <summary>
        /// 调用委托，得到数据绑定到控件
        /// </summary>
        /// <param name="size"></param>
        public void AutoPageDataBind(int size)
        {
            try
            {
                if (GetData != null)
                {
                    int tcount = 0;
                    Repeater.DataSource = GetData(CurPageIndex, size, out tcount);
                    Repeater.DataBind();
                    TotalRecordCount = tcount;
                }
                lblCurrentPageIndex.Text = (CurPageIndex + 1).ToString();//设置当前第几页

                //如果总记录数除以页面大小余数>0 则总页面数+1

                if (TotalRecordCount % size > 0)
                    lblAllPageCount.Text = Convert.ToString(TotalRecordCount / size + 1);       //设置总页数
                else
                    lblAllPageCount.Text = Convert.ToString(TotalRecordCount / size);           //设置总页数


                lnkbtnPrev.Enabled = CurPageIndex > 0;
                lnkbtnNext.Enabled = CurPageIndex < (TotalRecordCount - 1) / PageSize;

                if (this.Format != null)
                    Format();
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat("Pager Bind Data To Control Exception:{0}", ex);
            }

        }

        /// <summary>
        /// 重新绑定数据源，指向第一页
        /// </summary>
        /// <param name="size"></param>
        /// <param name="rebind"></param>
        public void AutoPageDataBind(int size, bool rebind)
        {
            if (rebind)
                this.CurPageIndex = 0;
            AutoPageDataBind(size);
        }

        /// <summary>
        /// 选择每页显示数据的条目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurPageIndex = 0;
            ViewState["pagesize"] = Convert.ToInt32(this.drpPageSize.SelectedValue);
            AutoPageDataBind(PageSize);
        }


        #region 分页按钮事件
        //首页
        protected void lnkHeadPage_Click(object sender, EventArgs e)
        {
            CurPageIndex = 0;
            AutoPageDataBind(PageSize);

        }
        //上一页
        protected void lnkbtnPrev_Click(object sender, EventArgs e)
        {
            CurPageIndex--;
            AutoPageDataBind(PageSize);
        }
        //下一页
        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            CurPageIndex++;
            AutoPageDataBind(PageSize);
        }
        //末页
        protected void lnkbtnLastPage_Click(object sender, EventArgs e)
        {
            CurPageIndex = Convert.ToInt32(lblAllPageCount.Text) - 1;
            AutoPageDataBind(PageSize);
        }

        //跳转到指定页
        protected void lnkbtnGo_Click(object sender, EventArgs e)
        {
            int result = 0;
            if (int.TryParse(txtGoPage.Text, out result))
            {
                if (string.IsNullOrEmpty(txtGoPage.Text) || Convert.ToInt32(txtGoPage.Text) <= 0)
                {
                    return;
                }
                else
                {
                    int currentIndex = result;
                    int totalCount = Convert.ToInt32(lblAllPageCount.Text);
                    if (currentIndex > totalCount)
                    {
                        txtGoPage.Text = lblCurrentPageIndex.Text;
                        return;
                    }
                    else
                    {
                        CurPageIndex = currentIndex - 1;
                        AutoPageDataBind(PageSize);
                    }

                }

            }

        }
        #endregion
    }
}